<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - hashes.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2013  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_LSH_HAShES_Hh_
<font color='#0000FF'>#define</font> DLIB_LSH_HAShES_Hh_

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='hashes_abstract.h.html'>hashes_abstract.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../hash.h.html'>../hash.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../matrix.h.html'>../matrix.h</a>"

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>class</font> <b><a name='hash_similar_angles_64'></a>hash_similar_angles_64</b>
    <b>{</b>
    <font color='#0000FF'>public</font>:
        <b><a name='hash_similar_angles_64'></a>hash_similar_angles_64</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> : seed<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <b>{</b><b>}</b>

        <b><a name='hash_similar_angles_64'></a>hash_similar_angles_64</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> uint64 seed_ 
        <font face='Lucida Console'>)</font> : seed<font face='Lucida Console'>(</font>seed_<font face='Lucida Console'>)</font> <b>{</b><b>}</b>

        uint64 <b><a name='get_seed'></a>get_seed</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> seed; <b>}</b>


        <font color='#0000FF'>typedef</font> uint64 result_type;

        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
            <font color='#0000FF'>typename</font> sparse_vector_type
            <font color='#5555FF'>&gt;</font>
        <font color='#0000FF'>typename</font> disable_if<font color='#5555FF'>&lt;</font>is_matrix<font color='#5555FF'>&lt;</font>sparse_vector_type<font color='#5555FF'>&gt;</font>,uint64<font color='#5555FF'>&gt;</font>::type <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> sparse_vector_type<font color='#5555FF'>&amp;</font> v
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> sparse_vector_type::value_type::second_type scalar_type;

            uint64 temp <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> <font color='#979000'>64</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#009900'>// compute the dot product between v and a Gaussian random vector.
</font>                scalar_type val <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'>typename</font> sparse_vector_type::const_iterator j <font color='#5555FF'>=</font> v.<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; j <font color='#5555FF'>!</font><font color='#5555FF'>=</font> v.<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>j<font face='Lucida Console'>)</font>
                    val <font color='#5555FF'>+</font><font color='#5555FF'>=</font> j<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>second<font color='#5555FF'>*</font><font color='#BB00BB'>gaussian_random_hash</font><font face='Lucida Console'>(</font>j<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>first, i, seed<font face='Lucida Console'>)</font>;

                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>val <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                    temp <font color='#5555FF'>|</font><font color='#5555FF'>=</font> <font color='#979000'>1</font>;
                temp <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>1</font>;
            <b>}</b>
            <font color='#0000FF'>return</font> temp;
        <b>}</b>

        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> EXP<font color='#5555FF'>&gt;</font>
        uint64 <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font> 
            <font color='#0000FF'>const</font> matrix_exp<font color='#5555FF'>&lt;</font>EXP<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> v
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> EXP::type T;
            uint64 temp <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> <font color='#979000'>64</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>dot</font><font face='Lucida Console'>(</font>matrix_cast<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#BB00BB'>gaussian_randm</font><font face='Lucida Console'>(</font>v.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,<font color='#979000'>1</font>,i<font color='#5555FF'>+</font>seed<font color='#5555FF'>*</font><font color='#979000'>64</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>, v<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                    temp <font color='#5555FF'>|</font><font color='#5555FF'>=</font> <font color='#979000'>1</font>;
                temp <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>1</font>;
            <b>}</b>
            <font color='#0000FF'>return</font> temp;
        <b>}</b>

        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>int</u></font> <b><a name='distance'></a>distance</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> result_type<font color='#5555FF'>&amp;</font> a,
            <font color='#0000FF'>const</font> result_type<font color='#5555FF'>&amp;</font> b
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#0000FF'>return</font> <font color='#BB00BB'>hamming_distance</font><font face='Lucida Console'>(</font>a,b<font face='Lucida Console'>)</font>;
        <b>}</b>

    <font color='#0000FF'>private</font>:
        <font color='#0000FF'>const</font> uint64 seed;
    <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>class</font> <b><a name='hash_similar_angles_128'></a>hash_similar_angles_128</b>
    <b>{</b>
    <font color='#0000FF'>public</font>:
        <b><a name='hash_similar_angles_128'></a>hash_similar_angles_128</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> : seed<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,hasher1<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>, hasher2<font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <b>{</b><b>}</b>

        <b><a name='hash_similar_angles_128'></a>hash_similar_angles_128</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> uint64 seed_
        <font face='Lucida Console'>)</font> : seed<font face='Lucida Console'>(</font>seed_<font face='Lucida Console'>)</font>,hasher1<font face='Lucida Console'>(</font><font color='#979000'>2</font><font color='#5555FF'>*</font>seed<font face='Lucida Console'>)</font>,hasher2<font face='Lucida Console'>(</font><font color='#979000'>2</font><font color='#5555FF'>*</font>seed<font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <b>{</b><b>}</b>

        uint64 <b><a name='get_seed'></a>get_seed</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> seed; <b>}</b>

        <font color='#0000FF'>typedef</font> std::pair<font color='#5555FF'>&lt;</font>uint64,uint64<font color='#5555FF'>&gt;</font> result_type;

        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
            <font color='#0000FF'>typename</font> vector_type 
            <font color='#5555FF'>&gt;</font>
        result_type <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> vector_type<font color='#5555FF'>&amp;</font> v
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#0000FF'>return</font> std::<font color='#BB00BB'>make_pair</font><font face='Lucida Console'>(</font><font color='#BB00BB'>hasher1</font><font face='Lucida Console'>(</font>v<font face='Lucida Console'>)</font>, <font color='#BB00BB'>hasher2</font><font face='Lucida Console'>(</font>v<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>int</u></font> <b><a name='distance'></a>distance</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> result_type<font color='#5555FF'>&amp;</font> a,
            <font color='#0000FF'>const</font> result_type<font color='#5555FF'>&amp;</font> b
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#0000FF'>return</font> <font color='#BB00BB'>hamming_distance</font><font face='Lucida Console'>(</font>a.first,b.first<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> 
                   <font color='#BB00BB'>hamming_distance</font><font face='Lucida Console'>(</font>a.second,b.second<font face='Lucida Console'>)</font>;
        <b>}</b>

    <font color='#0000FF'>private</font>:
        <font color='#0000FF'>const</font> uint64 seed;
        hash_similar_angles_64 hasher1;
        hash_similar_angles_64 hasher2;

    <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>class</font> <b><a name='hash_similar_angles_256'></a>hash_similar_angles_256</b>
    <b>{</b>
    <font color='#0000FF'>public</font>:
        <b><a name='hash_similar_angles_256'></a>hash_similar_angles_256</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> : seed<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>, hasher1<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>, hasher2<font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <b>{</b><b>}</b>

        <b><a name='hash_similar_angles_256'></a>hash_similar_angles_256</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> uint64 seed_
        <font face='Lucida Console'>)</font> : seed<font face='Lucida Console'>(</font>seed_<font face='Lucida Console'>)</font>,hasher1<font face='Lucida Console'>(</font><font color='#979000'>2</font><font color='#5555FF'>*</font>seed<font face='Lucida Console'>)</font>,hasher2<font face='Lucida Console'>(</font><font color='#979000'>2</font><font color='#5555FF'>*</font>seed<font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <b>{</b><b>}</b>

        uint64 <b><a name='get_seed'></a>get_seed</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> seed; <b>}</b>

        <font color='#0000FF'>typedef</font> std::pair<font color='#5555FF'>&lt;</font>uint64,uint64<font color='#5555FF'>&gt;</font> hash128_type;
        <font color='#0000FF'>typedef</font> std::pair<font color='#5555FF'>&lt;</font>hash128_type,hash128_type<font color='#5555FF'>&gt;</font> result_type;

        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
            <font color='#0000FF'>typename</font> vector_type 
            <font color='#5555FF'>&gt;</font>
        result_type <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> vector_type<font color='#5555FF'>&amp;</font> v
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#0000FF'>return</font> std::<font color='#BB00BB'>make_pair</font><font face='Lucida Console'>(</font><font color='#BB00BB'>hasher1</font><font face='Lucida Console'>(</font>v<font face='Lucida Console'>)</font>, <font color='#BB00BB'>hasher2</font><font face='Lucida Console'>(</font>v<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>int</u></font> <b><a name='distance'></a>distance</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> result_type<font color='#5555FF'>&amp;</font> a,
            <font color='#0000FF'>const</font> result_type<font color='#5555FF'>&amp;</font> b
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#0000FF'>return</font> hasher1.<font color='#BB00BB'>distance</font><font face='Lucida Console'>(</font>a.first,b.first<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> 
                   hasher1.<font color='#BB00BB'>distance</font><font face='Lucida Console'>(</font>a.second,b.second<font face='Lucida Console'>)</font>;
        <b>}</b>

    <font color='#0000FF'>private</font>:
        <font color='#0000FF'>const</font> uint64 seed;
        hash_similar_angles_128 hasher1;
        hash_similar_angles_128 hasher2;

    <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>class</font> <b><a name='hash_similar_angles_512'></a>hash_similar_angles_512</b>
    <b>{</b>
    <font color='#0000FF'>public</font>:
        <b><a name='hash_similar_angles_512'></a>hash_similar_angles_512</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> : seed<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>, hasher1<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>, hasher2<font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <b>{</b><b>}</b>

        <b><a name='hash_similar_angles_512'></a>hash_similar_angles_512</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> uint64 seed_
        <font face='Lucida Console'>)</font> : seed<font face='Lucida Console'>(</font>seed_<font face='Lucida Console'>)</font>,hasher1<font face='Lucida Console'>(</font><font color='#979000'>2</font><font color='#5555FF'>*</font>seed<font face='Lucida Console'>)</font>,hasher2<font face='Lucida Console'>(</font><font color='#979000'>2</font><font color='#5555FF'>*</font>seed<font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <b>{</b><b>}</b>

        uint64 <b><a name='get_seed'></a>get_seed</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> seed; <b>}</b>


        <font color='#0000FF'>typedef</font> hash_similar_angles_256::result_type hash256_type;
        <font color='#0000FF'>typedef</font> std::pair<font color='#5555FF'>&lt;</font>hash256_type,hash256_type<font color='#5555FF'>&gt;</font> result_type;

        <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
            <font color='#0000FF'>typename</font> vector_type 
            <font color='#5555FF'>&gt;</font>
        result_type <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> vector_type<font color='#5555FF'>&amp;</font> v
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#0000FF'>return</font> std::<font color='#BB00BB'>make_pair</font><font face='Lucida Console'>(</font><font color='#BB00BB'>hasher1</font><font face='Lucida Console'>(</font>v<font face='Lucida Console'>)</font>, <font color='#BB00BB'>hasher2</font><font face='Lucida Console'>(</font>v<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>int</u></font> <b><a name='distance'></a>distance</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'>const</font> result_type<font color='#5555FF'>&amp;</font> a,
            <font color='#0000FF'>const</font> result_type<font color='#5555FF'>&amp;</font> b
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#0000FF'>return</font> hasher1.<font color='#BB00BB'>distance</font><font face='Lucida Console'>(</font>a.first,b.first<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> 
                   hasher1.<font color='#BB00BB'>distance</font><font face='Lucida Console'>(</font>a.second,b.second<font face='Lucida Console'>)</font>;
        <b>}</b>

    <font color='#0000FF'>private</font>:
        <font color='#0000FF'>const</font> uint64 seed;
        hash_similar_angles_256 hasher1;
        hash_similar_angles_256 hasher2;
    <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_LSH_HAShES_Hh_
</font>

</pre></body></html>